//
//  MovieDetailViewController.m
//  UI豆瓣2
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetail.h"
#import "UIImageView+WebCache.h"
#import "DataBaseCollect.h"
#import "LoginViewController.h"
#import "Movie.h"
@interface MovieDetailViewController ()
@property (nonatomic, retain)MovieDetail *movieDetail;
@property (nonatomic, retain)NSString *tempMovName;
@end

@implementation MovieDetailViewController

- (void)dealloc
{
    [_movieDetail release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //返回按钮
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
    //收藏按钮
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    self.movieDetail = [[MovieDetail alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    //拼接地址,获取数据
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@", self.movieID];
    [AppTools getDataWithPath:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSDictionary *resultDic = [dic objectForKey:@"result"];
        self.navigationItem.title = [resultDic objectForKey:@"title"];
        self.movieDetail.ratingLabel.text = [NSString stringWithFormat:@"评分:%@",[resultDic objectForKey:@"rating"]];//评分
        self.movieDetail.rating_countLabel.text = [NSString stringWithFormat:@"(%@评论)",[resultDic objectForKey:@"rating_count"]];
        self.movieDetail.dataLabel.text = [resultDic objectForKey:@"release_date"];
        self.movieDetail.runtimeLabel.text = [resultDic objectForKey:@"runtime"];
        self.movieDetail.genresLabel.text = [resultDic objectForKey:@"genres"];
        self.movieDetail.countryLabel.text = [resultDic objectForKey:@"country"];
        self.movieDetail.actorsLabel.text = [resultDic objectForKey:@"actors"];
        self.movieDetail.plotLabel.text = [resultDic objectForKey:@"plot_simple"];
        CGFloat height = [[self class] heightForContent:[resultDic objectForKey:@"plot_simple"]];
        CGRect tempFrame = self.movieDetail.plotLabel.frame;
        tempFrame.size.height = height;
        self.movieDetail.plotLabel.frame = tempFrame;
        
        NSURL *url = [NSURL URLWithString:[resultDic objectForKey:@"poster"]];
        [self.movieDetail.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picholder.png"]];
        self.movieDetail.movieFavorite.contentSize = CGSizeMake(375, tempFrame.size.height + 515);
        self.movieDetail.movieFavorite.contentOffset = CGPointMake(0, 0);
        
    }];
    
    [self.view addSubview:self.movieDetail.movieFavorite];
    
    [_movieDetail release];
}

- (void)leftAction:(UIBarButtonItem *)leftBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightAction:(UIBarButtonItem *)rightBtn{
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    //判断是否登录,如果登录过,可直接收藏,否则跳转到登录
    
    NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
    //读取数据
    NSString *isLogin = [loginUserDef stringForKey:@"isLogin"];
    //    NSLog(@"loginVC.name = %@", loginVC.name);
    
    //如果当前登录状态isLogin = 1,则执行收藏的方法
    if ([isLogin isEqualToString:@"1"] ) {
        
        [[DataBaseCollect shareDataBase] openDB];
        [[DataBaseCollect shareDataBase] createTable];
        
        
        NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
        //登录时,取出当前的用户名是多少
        NSString *name = [loginUserDef stringForKey:@"name"];
        
        //搜索收藏数据库,查找名字为self.tem的电影并且为当前用户名的所有的电影,如果查找出来的数组为空,就收藏,否则不收藏
        NSMutableArray *arr = [[DataBaseCollect shareDataBase] selectMovieCollect:name MovieName:self.navigationItem.title];
        
        NSLog(@"title = %@", self.navigationItem.title);
        
        if (arr.count == 0) {
            
            NSLog(@"当前用户名name====== %@", name);
            
            //插入收藏的电影信息和当前用户名
            [[DataBaseCollect shareDataBase] insertMovie:self.movieID name:self.navigationItem.title user:name];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消");
            }];
            UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"OK");
            }];
            [alert addAction:canact];
            [alert addAction:othact];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
        else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"已收藏" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消");
            }];
            UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"OK");
            }];
            [alert addAction:canact];
            [alert addAction:othact];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
    //如果isLogin != 1,就跳转到登陆界面
    else{
        
        //跳转后隐藏tabbar
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
    }
    
    [loginVC release];

}

+ (CGFloat)heightForContent:(NSString *)content{
    //参数1.设置计算高度按照什么样的size来计算
    //注意:宽度一定于label宽度一致,高度尽量大一些
    //参数2.设置计算类型
    //参数3.设置计算内容按照多大的字体计算
    //注意:字体大小也要跟label一样
    // NSFontAttributeName
    //参数4.系统备用参数,设置为nil
    CGSize size = CGSizeMake(320, 10000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0],NSFontAttributeName, nil];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
